home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 310_02 / parse2.c < prev    next >
Text File  |  1990-04-18  |  13KB  |  604 lines

  1. /*
  2.     Little Smalltalk 
  3.         pass 2 of the parser
  4.  
  5.         timothy a. budd, 10/84
  6.  
  7. */
  8. /*
  9.     The source code for the Little Smalltalk System may be freely
  10.     copied provided that the source of all files is acknowledged
  11.     and that this condition is copied with each file.
  12.  
  13.     The Little Smalltalk System is distributed without responsibility
  14.     for the performance of the program and without any guarantee of
  15.     maintenance.
  16.  
  17.     All questions concerning Little Smalltalk should be addressed to:
  18.  
  19.         Professor Tim Budd
  20.         Department of Computer Science
  21.         Oregon State University
  22.         Corvallis, Oregon
  23.         97331
  24.         USA
  25. */
  26. # include <stdio.h>
  27. # include "env.h"
  28. # include "drive.h"
  29. # include "cmds.h"
  30. # include "parser.h"
  31. # include "ytab.h"
  32.  
  33. extern int maxcontext;
  34. extern char *filename;
  35. extern FILE *ofd;
  36.  
  37. static int inblock = 0;
  38. static int topstack = 0;
  39. static int maxstack = 0;
  40. # define bumpstk() if (++topstack > maxstack) maxstack = topstack
  41. # define popstk(i) topstack -= i
  42.  
  43. genclass(clinfo, mlist)
  44. struct classstruct *clinfo;
  45. struct methodstruct *mlist;
  46. {    int i;
  47.     struct methodstruct *m, *n;
  48.  
  49.     topstack = 0;
  50.     maxstack = 0;
  51.  
  52.     /* first find out how many methods have been declared */
  53.     /* also check for multiply defined methods */
  54.     for (m = mlist, i = 0; m; i++, m = m->nextmethod)
  55.         for (n = m->nextmethod; n; n = n->nextmethod)
  56.             if (streq((m->pattern)->cmdname ,
  57.                 (n->pattern)->cmdname))
  58.                 yerr("%s multiply defined",
  59.                     (m->pattern)->cmdname);
  60.  
  61.     fprintf(ofd,"temp <- <primitive 110 %d >\n", i);
  62.  
  63.     /* next print out each method */
  64.     for (m = mlist, i = 1; m; i++, m = m->nextmethod) {
  65.         fprintf(ofd,"<primitive 112 temp %d\t\t\" %s \" \\\n", 
  66.             i, (m->pattern)->cmdname);
  67.         topstack = 0;
  68.         genmeth(m);
  69.         }
  70.  
  71.     /* finally print out class definition stuff */
  72.     fprintf(ofd,"<primitive 98 #%s \\\n", clinfo->name);
  73.     fprintf(ofd,"\t<primitive 97 #%s #%s #%s \\\n", 
  74.         clinfo->name, clinfo->super, filename);
  75.     fprintf(ofd,"\t#( ");
  76.     if (instvars) prvars(instvars);
  77.     fprintf(ofd," ) \\\n");
  78.     fprintf(ofd,"\t#( ");
  79.     for (m = mlist; m; m = m->nextmethod)
  80.         fprintf(ofd,"#%s ", (m->pattern)->cmdname);
  81.     fprintf(ofd," ) \\\n");
  82.     fprintf(ofd,"\ttemp %d %d > >\n\n", 1 + maxcontext, 1 + maxstack);
  83. }
  84.  
  85. prvars(v)
  86. struct varstruct *v;
  87. {
  88.     if (v->nextvar)
  89.         prvars(v->nextvar);
  90.     fprintf(ofd," #%s", v->text);
  91. }
  92.  
  93. static int codetop = 0;
  94. static uchar code[1000];
  95.  
  96. static gencode(value)
  97. int value;
  98. {
  99.    if (value >= 256) {
  100.     yerr("code word too big: %d", value);
  101.     }
  102.    code[codetop++] = itouc(value);
  103. }
  104.  
  105. static genhighlow(high, low)
  106. int high, low;
  107. {
  108.    if (high < 0 || high > 16)
  109.     yerr("genhighlow error: %d", high);
  110.    if (low < 16) gencode(high * 16 + low);
  111.    else {
  112.       gencode(TWOBIT * 16 + high);
  113.       gencode(low);
  114.       }
  115. }
  116.  
  117. static struct litstruct *literals[100];
  118. int littop = 0;
  119.  
  120. int litcomp(l1, l2)
  121. struct litstruct *l1, *l2;
  122. {
  123.    if (l1->littype != l2->littype) return(0);
  124.    switch(l1->littype) {
  125.       case charlit: if (l1->ll.litchar != l2->ll.litchar) return(0); break;
  126.       case numlit: if (l1->ll.litint != l2->ll.litint) return(0); break;
  127.       case fnumlit: if (l1->ll.litstr != l2->ll.litstr) return(0); break;
  128.       case strlit:  if (strcmp(l1->ll.litstr, l2->ll.litstr)) return(0); break;
  129.       case symlit:  if (strcmp(l1->ll.litsym, l2->ll.litsym)) return(0); break;
  130.       default: return(0);
  131.       }
  132.    return(1);
  133. }
  134.  
  135. int genlitix(l)
  136. struct litstruct *l;
  137. {    int i;
  138.  
  139.     for (i = 0; i < littop; i++)
  140.         if (litcomp(l, literals[i]))
  141.             return(i);
  142.     i = littop;
  143.     literals[littop++] = l;
  144.     return(i);
  145. }
  146.  
  147. static printalit(lit)
  148. struct litlist *lit;
  149. {
  150.    if (lit) {
  151.       if(lit->nextlit)
  152.         printalit(lit->nextlit);
  153.       printlit(lit->litele);
  154.       }
  155. }
  156.  
  157. printlit(lit)
  158. struct litstruct *lit;
  159. {
  160.    if (lit)
  161.       switch(lit->littype) {
  162.          case numlit: fprintf(ofd,"%d ", lit->ll.litint); break;
  163.          case fnumlit: fprintf(ofd,"%s ", lit->ll.litstr); break;
  164.          case charlit: fprintf(ofd,"$%c ", lit->ll.litchar); break;
  165.          case strlit:  fprintf(ofd,"\'%s\' ", lit->ll.litstr); break;
  166.          case symlit:  fprintf(ofd,"#%s ", lit->ll.litsym); break;
  167.          case arlit:   fprintf(ofd,"#( ");
  168.                        printalit(lit->ll.litarry);
  169.                        fprintf(ofd,") ");
  170.                        break;
  171.          default: yerr("unknown literal type %d", lit->littype);
  172.          }
  173. }
  174.  
  175. genmeth(m)
  176. struct methodstruct *m;
  177. {     int i;
  178.  
  179.     fprintf(ofd,"\t#( #[");
  180.     codetop = littop = 0;
  181.     genstate(m->states, 1);
  182.     genhighlow(SPECIAL, SELFRETURN);
  183.     for (i = 0; i < codetop; i++){
  184.            fprintf(ofd," %d", uctoi(code[i]));
  185.         if (i % 15 == 14) fprintf(ofd," \\\n");
  186.         }
  187.     fprintf(ofd,"] \\\n");
  188.     fprintf(ofd,"\t#( ");
  189.     for (i = 0; i < littop; i++)
  190.         printlit(literals[i]);
  191.     fprintf(ofd," ) ) >\n\n");
  192. }
  193.  
  194. genstate(s, doret)
  195. struct statestruct *s;
  196. int doret;
  197. {
  198.     if (s->nextstate)
  199.         genstate(s->nextstate, doret);
  200.     switch(s->statetype) {
  201.         default:
  202.             yerr("unknown case in genstate %d", s->statetype);
  203.         case blkupar:
  204.             gensexpr(s->nn.stateexpr);
  205.             if (inblock)
  206.                 genhighlow(SPECIAL, BLOCKRETURN);
  207.             else
  208.                 genhighlow(SPECIAL, RETURN);
  209.             popstk(1);
  210.             break;
  211.         case upar:
  212.             gensexpr(s->nn.stateexpr);
  213.             if (doret)
  214.                 genhighlow(SPECIAL, RETURN);
  215.             popstk(1);
  216.             break;
  217.         case iasgn:
  218.             gensexpr(s->nn.stateexpr);
  219.             genhighlow(POPINSTANCE, s->mm.varpos);
  220.             popstk(1);
  221.             break;
  222.         case casgn:
  223.             gensexpr(s->nn.stateexpr);
  224.             genhighlow(POPTEMP, s->mm.varpos);
  225.             popstk(1);
  226.             break;
  227.         case expr:
  228.             genexpr(s->nn.cmd);
  229.             genhighlow(SPECIAL, POPSTACK);
  230.             popstk(1);
  231.             break;
  232.         }
  233. }
  234.  
  235. gensexpr(s)
  236. struct statestruct *s;
  237. {
  238.     switch(s->statetype) {
  239.         default:
  240.             yerr("unknown state in gensexpr %d", s->statetype);
  241.         case iasgn:
  242.             gensexpr(s->nn.stateexpr);
  243.             genhighlow(SPECIAL, DUPSTACK);
  244.             bumpstk();
  245.             genhighlow(POPINSTANCE, s->mm.varpos);
  246.             popstk(1);
  247.             break;
  248.         case casgn:
  249.             gensexpr(s->nn.stateexpr);
  250.             genhighlow(SPECIAL, DUPSTACK);
  251.             bumpstk();
  252.             genhighlow(POPTEMP, s->mm.varpos);
  253.             popstk(1);
  254.             break;
  255.         case expr:
  256.             genexpr(s->nn.cmd);
  257.             break;
  258.         }
  259. }
  260.  
  261. int supertest(rec)
  262. struct exprstruct *rec;
  263. {    struct objstruct *o;
  264.  
  265.     if (rec->cmdtype != reccmd)
  266.         return(0);
  267.     o = rec->cc.recobj;
  268.     if (o->objtype != pseuobj)
  269.         return(0);
  270.     if (o->ee.pseuinfo == supervar)
  271.         return(1);
  272.     return(0);
  273. }
  274.  
  275. int isblock(e)
  276. struct exprstruct *e;
  277. {    struct objstruct *o;
  278.  
  279.     if (e->cmdtype != reccmd)
  280.         return(0);
  281.     o = e->cc.recobj;
  282.     if (o->objtype != blockobj)
  283.         return(0);
  284.     return(1);
  285. }
  286.  
  287. genbarg(e)
  288. struct exprstruct *e;
  289. {
  290.     if (isblock(e)) {
  291.         genstate(((e->cc.recobj)->ee.blockinfo)->bstates, 0);
  292.         }
  293.     else {
  294.         genexpr(e);
  295.         genhighlow(UNSEND, VALUECMD);
  296.         }
  297. }
  298.  
  299. fixjump(loc)
  300. int loc;
  301. {    int size;
  302.     
  303.     size = (codetop - loc) - 1;
  304.     if (size > 255)
  305.         yerr("block too big %d", size);
  306.     code[loc] = itouc(size);
  307. }
  308.  
  309. int gencond(message, e)
  310. char *message;
  311. struct exprstruct *e;
  312. {    struct keylist *k;
  313.     int i, j;
  314.  
  315.      k = e->cc.keys;
  316.     i = 0;
  317.     if ((i = streq(message, "ifTrue:")) || streq(message, "ifFalse:")) {
  318.         genhighlow(SPECIAL, i ? SKIPFALSEPUSH : SKIPTRUEPUSH);
  319.         i = codetop;
  320.         gencode(0);
  321.         genbarg(k->arg);
  322.         fixjump(i);
  323.         return(1);
  324.         }
  325.     if ((i = streq(message, "ifTrue:ifFalse:")) || 
  326.             streq(message, "ifFalse:ifTrue:")) {
  327.         genhighlow(SPECIAL, i ? SKIPFALSEPUSH : SKIPTRUEPUSH);
  328.         i = codetop;
  329.         gencode(0);
  330.         genbarg((k->nextkey)->arg);
  331.         genhighlow(SPECIAL, SKIPFORWARD);
  332.         j = codetop;
  333.         gencode(0);
  334.         fixjump(i);
  335.         genhighlow(SPECIAL, POPSTACK);
  336.         popstk(1);
  337.         genbarg(k->arg);
  338.         fixjump(j);
  339.         return(1);
  340.         }
  341.     if ((i = streq(message, "and:")) || streq(message, "or:")) {
  342.         genhighlow(SPECIAL, i ? SKIPF : SKIPT);
  343.         i = codetop;
  344.         gencode(0);
  345.         genbarg(k->arg);
  346.         fixjump(i);
  347.         return(1);
  348.         }
  349.     if ((j = streq(message, "whileTrue:")) ||
  350.             streq(message, "whileFalse:")) {
  351.         i = codetop;
  352.         genbarg(e->receiver);
  353.         genhighlow(SPECIAL, j ? SKIPFALSEPUSH : SKIPTRUEPUSH);
  354.         j = codetop;
  355.         gencode(0);
  356.         genbarg(k->arg);
  357.         genhighlow(